package org.fuys.coder.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fuys.coder.common.entity.PageDTO;
import org.fuys.coder.domain.act.lottery.model.req.award.AwardAddReq;
import org.fuys.coder.domain.act.lottery.model.vo.award.AwardVO;
import org.fuys.coder.domain.act.lottery.repository.IAwardRepository;
import org.fuys.coder.infrastructure.dao.AwardMapper;
import org.fuys.coder.infrastructure.model.po.Award;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.repository
 * @className: AwardRepository
 * @author: WangJing
 * @description: 奖品仓储服务
 * @date: 2024/7/5 22:55
 * @version: 1.0
 */
@Service
public class AwardRepository extends ServiceImpl<AwardMapper, Award>
        implements IAwardRepository {

    @Resource
    private AwardMapper awardMapper;

    @Override
    public void addAward(AwardAddReq req) {
        Award award=new Award();
        award.setAwardName(req.getAwardName());
        award.setAwardDesc(req.getDesc());
        award.setAwardCover(req.getCover());
        award.setAwardContent(req.getContent());
        award.setAwardType(req.getType());
        award.setStatus(0);
        awardMapper.insert(award);
        req.setId(award.getId());
    }

    @Override
    public void updateStatusById(Long id,Integer status) {
        awardMapper.update(null,new LambdaUpdateWrapper<Award>()
                .eq(Award::getId,id).set(Award::getStatus,status));
    }

    @Override
    public List<AwardVO> getPage(PageDTO pageDTO,Long id,String name) {
        Page<Award> page=new Page<>(pageDTO.getCurPage(),pageDTO.getPageNum());
        final Page<Award> awardPage = awardMapper.selectPage(page, new LambdaUpdateWrapper<Award>()
                .eq(id!=null,Award::getId,id).like(name!=null,Award::getAwardName,name));
        if(ObjectUtils.isEmpty(awardPage)||ObjectUtils.isEmpty(awardPage.getRecords())){
            return null;
        }
        return awardPage.getRecords().stream().map(item -> {
            AwardVO awardVO = new AwardVO();
            awardVO.setAwardId(item.getId());
            awardVO.setAwardName(item.getAwardName());
            awardVO.setAwardDesc(item.getAwardDesc());
            awardVO.setAwardCover(item.getAwardCover());
            awardVO.setAwardType(item.getAwardType());
            awardVO.setAwardContent(item.getAwardContent());
            awardVO.setCreatetime(item.getCreatetime());
            awardVO.setUpdatetime(item.getUpdatetime());
            return awardVO;
        }).collect(Collectors.toList());
    }
}
